package com.schoolhelper.controller.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.schoolhelper.controller.activity.R;
import com.schoolhelper.model.adaptermodel.MainRecyclerViewContent;

import java.util.List;

public class MainRecyclerViewContentAdapter extends RecyclerView.Adapter<MainRecyclerViewContentAdapter.ViewHolder> implements View.OnClickListener {

    private int contentType = 2;

    private List<MainRecyclerViewContent> contentsList;

    private OnItemClickListener itemClickListener;

    public MainRecyclerViewContentAdapter(List<MainRecyclerViewContent> contents){
        contentsList = contents;
    }

    public int getContentType() {
        return contentType;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView title;
        TextView content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.main_content_img);
            title = (TextView) itemView.findViewById(R.id.main_recyclerview_content_title);
            content = (TextView) itemView.findViewById(R.id.main_recyclerview_content);

        }
    }

    @Override
    public void onClick(View v) {
        if (itemClickListener != null){
            itemClickListener.onItemClick((Integer) v.getTag(), contentType);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_content_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MainRecyclerViewContent content = contentsList.get(position);
        holder.imageView.setImageBitmap(content.getImagesrc());
        holder.title.setText(content.getTitle());
        holder.content.setText(content.getContent());

        holder.itemView.setTag(position);

//        System.out.println("******************************************************************************");
//        System.out.println(position);
//        System.out.println("******************************************************************************");

    }

    @Override
    public int getItemCount() {
        return contentsList.size();
    }

    public void setItemClickListener(OnItemClickListener onItemClickListener, int mcontentType){
        itemClickListener = onItemClickListener;
    }


}
